/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-06-30 00:24:01
 * @LastEditors: paddies
 * @LastEditTime: 2024-12-22 18:51:54
 * @FilePath: /shared_components/lib/luntai-components/build-self-store-panel/buildSelfStorePanel.dart
 */
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-button/buildButton.dart';
import 'package:shared_components/build-common/buildCommon.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/build-line/buildLine.dart';
import 'package:url_launcher/url_launcher.dart';

Widget buildSelfStorePanel(
    {dynamic item,
    Function? collectionsFn,
    required Function openPanel,
    required Function startEvaluate}) {
  var feeInfos = [].obs;
  var baseInfos = [].obs;
  var orderItems = [].obs;
  var defauleColor = Color(0xff000000);
  if (item["orderItems"] != null) {
    orderItems.value = item["orderItems"];
  }
  if (item["feeInfos"] != null) {
    feeInfos.value = item["feeInfos"];
  }
  if (item["baseInfos"] != null) {
    baseInfos.value = item["baseInfos"];
  }
  return Column(
    children: [
      Container(
        width: double.infinity,
        // height: ,
        color: const Color(0xff5273c5),
        padding:
            const EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 10),
        child: const Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              "交易完成",
              style: TextStyle(fontSize: 16, color: Colors.white),
            ),
            Text("感谢你的信任，欢迎你再次光临！",
                style: TextStyle(fontSize: 16, color: Colors.white))
          ],
        ),
      ),
      ...orderItems.value.map(((e) {
        return Column(
          children: [
            Container(
                width: double.infinity,
                padding: EdgeInsets.all(5),
                height: 100,
                decoration: new BoxDecoration(
                  color: Colors.white,
                  // borderRadius: new BorderRadius.all(
                  //     new Radius.circular(10.0)), //圆角
                ),
                child: Column(
                  children: [
                    Expanded(
                      child: Row(
                        children: [
                          Container(
                            width: 100,
                            height: double.infinity,
                            // color: Colors.blue,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Container(
                                  width: 90,
                                  height: 90,
                                  child: buildImage(
                                      type: "ClipRect",
                                      fit: BoxFit.fill,
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(5)),
                                      url: e["skuPicUrl"].toString()),
                                  // color: Colors.yellow,
                                )
                              ],
                            ),
                          ),
                          Expanded(
                              child: Container(
                            width: double.infinity,
                            height: double.infinity,
                            padding: EdgeInsets.only(right: 5, left: 5),
                            // color: Colors.amberAccent,
                            child: Column(
                              children: [
                                Container(
                                  width: double.infinity,
                                  // height: 40,
                                  // color: Colors.green,
                                  child: Text(
                                    e["productName".toString()],
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 14),
                                  ),
                                ),
                                Container(
                                  width: double.infinity,
                                  // height: 25,
                                  alignment: Alignment.centerLeft,
                                  // color: Colors.red,
                                  child: Text(
                                    showText(e["size"]) +
                                        " " +
                                        showText(e["color"]),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                        fontSize: 13, color: Color(0xff666666)),
                                  ),
                                ),
                                // Visibility(
                                //     visible: showText(e["tireSize"]) != '',
                                //     child: Container(
                                //       width: double.infinity,
                                //       child: Row(
                                //         children: [
                                //           Text("胎码：" + showText(e["tireSize"]))
                                //         ],
                                //       ),
                                //     )),
                                Expanded(
                                    child: Container(
                                        width: double.infinity,
                                        height: double.infinity,
                                        // color: Colors.red,
                                        alignment: Alignment.centerLeft,
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text(
                                              "¥" + e["unitPrice"].toString(),
                                              style: TextStyle(
                                                  fontSize: 16,
                                                  fontWeight: FontWeight.bold,
                                                  color: Color(0xffFF4646)),
                                            ),
                                            Text(
                                              "x" + showText(e["quantity"]),
                                              style: TextStyle(fontSize: 13),
                                            )
                                          ],
                                        )))
                              ],
                            ),
                          )),
                        ],
                      ),
                    ),
                  ],
                )),
            Container(
              width: double.infinity,
              color: Colors.white,
              padding: EdgeInsets.only(bottom: 5, top: 5, right: 10, left: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Visibility(
                    visible: e["isComment"].toString() == '1',
                    child: GestureDetector(
                        onTap: () {
                          openPanel(e);
                        },
                        child: Text(
                          "已评价",
                          style: TextStyle(color: Color(0xff999999)),
                        )),
                  ),
                  Visibility(
                      visible: e["isComment"].toString() == '0',
                      child: Container(
                        width: 80,
                        height: 30,
                        child: buildButton(
                            title: "评价",
                            borderRadius: 10,
                            fontSize: 14,
                            onClick: () {
                              startEvaluate(e);
                            },
                            colors: [Color(0xffee8a33), Color(0xffee8a33)]),
                      ))
                ],
              ),
            )
          ],
        );
      })).toList(),
      buildLine(),
      Visibility(
        visible: feeInfos.value.isNotEmpty,
        child: Column(
          children: [
            ...feeInfos.value.map((e) {
              return Container(
                width: double.infinity,
                // height: 50,
                color: Colors.white,
                padding:
                    EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      showText(e["name"]),
                      style:
                          TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                    ),
                    Text(
                      showText(e["value"]),
                      style: TextStyle(
                          fontSize: 14,
                          color: Color(0xffFF3333),
                          fontWeight: FontWeight.bold),
                    )
                  ],
                ),
              );
            })
          ],
        ),
      ),
      buildLine(),
      Visibility(
        visible: baseInfos.value.isNotEmpty,
        child: Column(
          children: [
            ...baseInfos.value.map((e) {
              return Container(
                width: double.infinity,
                // height: 50,
                color: Colors.white,
                padding:
                    EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      showText(e["name"]),
                      style:
                          TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                    ),
                    Text(
                      showText(e["value"]),
                      style: TextStyle(
                          fontSize: 14,
                          color: showText(e["color"]) == ""
                              ? defauleColor
                              : Color(
                                  int.parse('0xff' +
                                      showText(e["color"].replaceAll("#", ""))),
                                ),
                          fontWeight: FontWeight.bold),
                    )
                  ],
                ),
              );
            })
          ],
        ),
      ),
      buildLine(),
      Container(
        width: double.infinity,
        height: 50,
        color: Colors.white,
        alignment: Alignment.center,
        child: Row(
          children: [
            Expanded(
                child: GestureDetector(
              onTap: () {
                openBaiduMap(
                    lng: double.parse(showText(item["x"])),
                    lat: double.parse(showText(item["y"])));
              },
              child: Container(
                alignment: Alignment.center,
                width: double.infinity,
                height: double.infinity,
                child: Text(
                  "到这里去",
                  style: TextStyle(color: Color(0xff5292f7)),
                ),
              ),
            )),
            Container(
              width: 1,
              height: 20,
              color: Color(0xffc2c2c2),
            ),
            Expanded(
                child: GestureDetector(
              onTap: () async {
                await launch('tel:' + showText(item["selfStorePhone"]));
              },
              child: Container(
                width: double.infinity,
                alignment: Alignment.center,
                height: double.infinity,
                child: Text(
                  "拨打电话",
                  style: TextStyle(color: Color(0xff5292f7)),
                ),
              ),
            ))
          ],
        ),
      ),
      // Text(showText(item["state"])),
      Container(
        width: double.infinity,
        height: 70,
        alignment: Alignment.centerRight,
        child: Visibility(
            visible: ['YWC'].contains(showText(item["state"])),
            // controller.state.state.value == '已完成',
            child: Container(
              width: 100,
              height: double.infinity,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              child: buildButton(
                  title: "收藏店铺",
                  fontSize: 16,
                  onClick: () async {
                    if (collectionsFn != null) {
                      collectionsFn();
                    }
                    // print(controller.state.orderDetail.value["storeId"]);
                    // var storeId =
                    //     showText(controller.state.orderDetail.value["storeId"]);
                    // await doAddCollections(bizId: storeId, collectionsType: "2");
                    // EasyLoading.showToast("收藏成功");
                    // controller.startEvaluate();
                  },
                  colors: [Color(0xffee8a33), Color(0xffee8a33)]),
            )),
      ),
      SizedBox(
        height: 40,
      )
    ],
  );
}

openBaiduMap({double lat = 0.0, double lng = 0.0, String title = ''}) async {
  var url =
      'baidumap://map/direction?destination=name:${title}|$lat,$lng&coord_type=bd09ll&mode=driving';
  if (Platform.isIOS) {
    url = Uri.encodeFull(url);
  }

  try {
    await launch(url);
  } catch (e) {
    EasyLoading.showToast('打开百度地图失败~');
  }
}
